package fr.upstlse.glre.client.espaceperso.profil;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.ui.Composite;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.espaceperso.EspaceEtudiant;
import fr.upstlse.glre.client.espaceperso.profil.action.StageEditionSubmit;
import fr.upstlse.glre.client.vo.user.EtudiantVOGWT;
import fr.upstlse.glre.client.vo.user.StageVOGWT;
import fr.upstlse.glre.client.widgets.fields.StageFields;

public class EtudiantStage extends Composite {

	ContentPanel stagePanel;


	Html stageReadingPanel;

	FormPanel stageEditingPanel;

	StageVOGWT stage;
	EtudiantVOGWT etudiant;

	boolean editionMode = false;

	public EtudiantStage() {
		etudiant = EspaceEtudiant.getInstance().getUser();
		stage = etudiant.getStage();

		createStagePanel();
		readingMode();
		editionMode();
		
		stagePanel.add(stageReadingPanel);
		editionMode = false;
		initWidget(stagePanel);
	}
	
	private void createStagePanel() {
		stagePanel = new ContentPanel();
		stagePanel.setAutoWidth(true);
		stagePanel.setWidth(500);
		stagePanel.setId(RfgConstante.STAGE_PANEL_ID);
		stagePanel.setBorders(true);

		// Draggable panel
		Draggable lDraggable = new Draggable(stagePanel, stagePanel
				.getHeader());

		// Tool Button
		ToolButton lSwithToEditionMode = new ToolButton(".my-tool-gear");
		lSwithToEditionMode.setStyleAttribute("background",
				" transparent url(images/edit.png) no-repeat");
		lSwithToEditionMode
				.setToolTip(RfgConstante.STAGE_PANEL_BUTTON_SWITCH_TOOLTIP);
		lSwithToEditionMode.addSelectionListener(new SelectionListener() {
			public void componentSelected(ComponentEvent ce) {
				if (!editionMode) {
					stagePanel.add(stageEditingPanel);
					stagePanel.layout();
					stagePanel.remove(stageReadingPanel);
					stagePanel.layout();
					editionMode = true;
				}
				else {
					stagePanel.add(stageReadingPanel);
					stagePanel.layout();
					stagePanel.remove(stageEditingPanel);
					stagePanel.layout();
					editionMode = false;
				}
			}

			public void handleEvent(BaseEvent be) {
				// TODO Auto-generated method stub
				
			}

		});
		stagePanel.setHeading(RfgConstante.STAGE_PANEL_HEADER_TEXT);
		stagePanel.getHeader().addTool(lSwithToEditionMode);
	}

	private void editionMode() {
		// formulaire sans action
		stageEditingPanel = new FormPanel();
		stageEditingPanel.setHeaderVisible(false);
		stageEditingPanel.setAutoWidth(true);

		StageFields lStageFields = new StageFields(stage, stageEditingPanel);	

		Button lSubmit = new Button(
				RfgConstante.PROFIL_EDITING_FIELD_SUBMIT_LABEL);
		lSubmit.addSelectionListener(new StageEditionSubmit(lStageFields, this));
		stageEditingPanel.addButton(lSubmit);
	}

	public void readingMode() {
		if (stage == null) {
			MessageBox lFailureMessageBox = MessageBox.alert(
					"Erreur accès au stage",
					"L'étudiant n'a pas de stage", null);
		} else {
			stageReadingPanel = stage.getHtml();
		}

	}

	public ContentPanel getStagePanel() {
		return stagePanel;
	}

	public void setStagePanel(ContentPanel stagePanel) {
		this.stagePanel = stagePanel;
	}

	public Html getStageReadingPanel() {
		return stageReadingPanel;
	}

	public void setStageReadingPanel(Html stageReadingPanel) {
		this.stageReadingPanel = stageReadingPanel;
	}

	public FormPanel getStageEditingPanel() {
		return stageEditingPanel;
	}

	public void setStageEditingPanel(FormPanel stageEditingPanel) {
		this.stageEditingPanel = stageEditingPanel;
	}

	public StageVOGWT getStage() {
		return stage;
	}

	public void setStage(StageVOGWT stage) {
		this.stage = stage;
	}

	public boolean isEditionMode() {
		return editionMode;
	}

	public void setEditionMode(boolean editionMode) {
		this.editionMode = editionMode;
	}

	public EtudiantVOGWT getEtudiant() {
		return etudiant;
	}

	public void setEtudiant(EtudiantVOGWT etudiant) {
		this.etudiant = etudiant;
	}
}
